package io.github.simplejy.miodemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import java.util.ArrayList;

import io.github.simplejy.miodemo.R;

/**
 * 当前参数
 */

public class CurrentParamActivity extends AppCompatActivity implements View.OnClickListener {

    private TableLayout tableLayout;
    private ArrayList<TableRow> tableRows;

    private TextView[] textViews;
    //EditView数组：为当前参数界面的输入框数组
    private EditText[] editTexts;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_param);
        initView();
    }

    private void initView() {

        tableLayout = (TableLayout) findViewById(R.id.table_layout_current);
        findViewById(R.id.layout_back_main).setOnClickListener(this);

        int count = tableLayout.getChildCount();
        tableRows = new ArrayList<>();
        textViews = new TextView[13];
        editTexts = new EditText[13];

        for (int i = 0; i < count; i++) {
            View v = tableLayout.getChildAt(i);
            if (v instanceof TableRow) {
                tableRows.add((TableRow) v);
                //为每一行设置点击监听
                v.setOnClickListener(this);
            }
        }

        //获取所有editText
        for (int i = 0; i < tableRows.size(); i++) {
            TableRow tableRow = tableRows.get(i);
            textViews[i] = (TextView) tableRow.getChildAt(0);
            editTexts[i] = (EditText) tableRow.getChildAt(1);
            //设置tag的目的是在点击监听中判断是点击的第几行
            tableRow.setTag(i);
            //设置所有为单行输入
            editTexts[i].setSingleLine();
        }
    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.layout_back_main) {
            finish();
        }

        if (v.getTag() == null) {
            return;
        }
        int index = (int) v.getTag();
        showCustomDialog(textViews[index].getText().toString());

    }

    //自定义的Dialog对话框
    private void showCustomDialog(String title) {
        View view = getLayoutInflater().inflate(R.layout.dialog_current_param, null);

        //设置对话框里的控件
        ((TextView) view.findViewById(R.id.tv_dialog_title)).setText(title);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);
        builder.create().show();

    }
}
